package cn.bytecollege.controller;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Room;
import cn.bytecollege.entity.UserManage;
import cn.bytecollege.entity.Vip;
import cn.bytecollege.service.RoomService;
import cn.bytecollege.service.UserMessageService;
import cn.bytecollege.service.VipService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author LH
 * @time 2021-10-29 15:34
 * 修改住户信息
 */
@WebServlet(name = "UserMessageChangeController", value = "/userMessageChange")
public class UserMessageChangeController extends HttpServlet {
    private VipService vipService=null;
    private UserMessageService userMessageService=null;
    private RoomService roomService=null;
    public UserMessageChangeController(){
        userMessageService=new UserMessageService();
        vipService=new VipService();
        roomService= new RoomService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        Integer orderId=Integer.valueOf(id);
        String username = request.getParameter("username");
        String phone = request.getParameter("phone");
        String idNumber = request.getParameter("idNumber");
        String roomNumber = request.getParameter("roomNumber");
        Room room=roomService.findByRoomNumber(Integer.valueOf(roomNumber));
        Double price=room.getRoomPrice();
        Integer roomId=room.getRoomId();
        Double truePrice=price;
        Vip vip=vipService.findByIdNumber(idNumber);
        if (vip!=null){
            UserManage userManage=new UserManage();
            truePrice=price*0.8;
            userManage.setOrderId(orderId);
            userManage.setIsVip(Integer.valueOf("1"));
            userManage.setUserIdNumber(idNumber);
            userManage.setUsername(username);
            userManage.setUserState(Integer.valueOf("1"));
            userManage.setUserStartTime(LocalDateTime.now());
            userManage.setTruePrice(truePrice);
            userManage.setUserPhoneNumber(phone);
            userManage.setRoomId(roomId);
            userMessageService.change(userManage);
            //将房间从未入住改为已入住。
            roomService.updateRoomStateTo1(roomId);
            request.setAttribute("truePrice",truePrice);
            request.getRequestDispatcher(Resource.PAGE_LOCATION+"/addSucceed.jsp").forward(request,response);
            return;

        }
        if (vip==null){
            UserManage userManage=new UserManage();
            userManage.setIsVip(Integer.valueOf("0"));
            userManage.setOrderId(orderId);
            userManage.setUserIdNumber(idNumber);
            userManage.setUsername(username);
            userManage.setUserState(Integer.valueOf("1"));
            userManage.setUserStartTime(LocalDateTime.now());
            userManage.setTruePrice(truePrice);
            userManage.setUserPhoneNumber(phone);
            userManage.setRoomId(roomId);
            userMessageService.change(userManage);
            //将房间从未入住改为已入住。
            roomService.updateRoomStateTo1(roomId);
            request.setAttribute("truePrice",truePrice);
            request.getRequestDispatcher(Resource.PAGE_LOCATION+"/addSucceed.jsp").forward(request,response);
            return;
        }
    }

}
